package com.ubtechinc.cruzr.libai.honglauncher;

import android.text.TextUtils;

import com.orhanobut.logger.Logger;
import com.ubtechinc.cruzr.sdk.entity.Nlu;
import com.ubtechinc.cruzr.sdk.entity.UnderstandResult;
import com.ubtechinc.cruzr.sdk.speech.ISpeechContext;
import com.ubtechinc.cruzr.sdk.speech.SpeechConstant;
import com.ubtechinc.cruzr.sdk.speech.SpeechRobotApi;
import com.ubtechinc.cruzr.serverlibutil.interfaces.InitListener;
import com.ubtechinc.cruzr.serverlibutil.interfaces.SpeechTtsListener;

/**
 * Created by ruiqiang on 2018/5/10.
 */

public class TtsManager {
    private TtsManager() {
    }

    public void init() {
        SpeechRobotApi.get().initializ(App.getContext(), App.APP_ID, new InitListener() {
            @Override
            public void onInit() {
                super.onInit();
                SpeechRobotApi.get().registerSpeech(new ISpeechContext() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onStop() {

                    }

                    @Override
                    public void onPause() {

                    }

                    @Override
                    public void onResume() {

                    }

                    @Override
                    public void onResult(String s) {
                        syncCommand(s);
                    }
                });
            }
        });
    }

    private static class ClassHolder {
        private final static TtsManager instance = new TtsManager();
    }

    public static TtsManager get() {
        return ClassHolder.instance;
    }

    public void speech(String content) {
        if (TextUtils.isEmpty(content)) {
            return;
        }
        Logger.e("开始播报 tts =  " + content);
        SpeechRobotApi.get().speechStartTTS(content, new SpeechTtsListener() {
            @Override
            public void onAbort() {
            }

            @Override
            public void onEnd() {
            }
        });
    }

    private void syncCommand(String s) {
        try {
            Logger.e("syncCommand: s = " + s);
            Nlu nlu = Nlu.getNlu(s);
            if (nlu == null || nlu.onlu == null) {
                return;
            }
            boolean isOnline = (SpeechConstant.COMMAND_ONLINE == nlu.onlu.command);
            if (isOnline) {
                final UnderstandResult.Message command = nlu.understand.obtainMessage("userDefine");
                if (command != null) {
                    String intent = command.getParameters().getString("intent");
                    Logger.e("syncCommand: intent =  = " + intent);
                    if (UbtCantans.OPEN_INTENT.equals(intent)) {
                        String packagename = command.getParameters().getString("package_name");
                        Logger.e("syncCommand: packagename =  = " + packagename);
                        if (!TextUtils.isEmpty(packagename)) {
//                            AppManger.get().syncTtsCommand(packagename);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e("sync nlu error");
        }
    }
}
